const Response = require("../../class/Response");

function transferMenu(pl){
    WSC.client.sendJson(
        new Response().setUrl("/bds/getOnlineClientsInfo"), (req, res) =>{
            // '/bds/getOnlineClientsInfo/result'
            if (pl == null){return;} // 玩家下线
            let onlineClientsInfo = req.data.onlineClientsInfo;

            let fm_transSrv = mc.newSimpleForm();
            fm_transSrv.setTitle("§c§l传送至同步服务器...");
            fm_transSrv.setContent(`\n§b选择一个服务器:\n`);
            let BDSs = [];
            for (let clientId in onlineClientsInfo){
                let bdsInfo = onlineClientsInfo[clientId];
                if (bdsInfo.userType != 'bds'){
                    continue;
                }
                let isThisBds = bdsInfo?.bds?.uuid == CONF.CONFIG.get('Websocket')['BDSUuid'];
                let btnText = `§小§l${isThisBds ? ' ' : ''}${bdsInfo.name}`;
                let btnExtraText = [];
                if (!CONF.CONFIG.get('PersonalConfig') || CONF.CONFIG.get('PersonalConfig')['TransferShowPlayersNum']==undefined || CONF.CONFIG.get('PersonalConfig')['TransferShowPlayersNum']){
                    let fullPlayersColor = Object.keys(bdsInfo.onlinePlayers).length >= bdsInfo.bds.maxPlayersNum-1 ? '§c' : '';
                    btnExtraText.push(`§r${fullPlayersColor}${Object.keys(bdsInfo.onlinePlayers).length}/${bdsInfo.bds.maxPlayersNum}§r`);
                }
                if (!CONF.CONFIG.get('PersonalConfig') || !CONF.CONFIG.get('PersonalConfig')['TransferShowTps']==undefined || CONF.CONFIG.get('PersonalConfig')['TransferShowTps']){
                    btnExtraText.push(`§r${getTpsColor(bdsInfo.bds.averageTps)}${bdsInfo.bds.averageTps.toFixed(1)}§r`);
                }
                if (btnExtraText.length > 0){
                    btnText += `\n` + btnExtraText.join(' | ');
                }

                fm_transSrv.addButton(btnText);
                BDSs.push(bdsInfo);
            }
            
            pl.sendForm(fm_transSrv, transSrv_run);

            function transSrv_run(pl, id){
                if (id == null){return;}
                if (BDSs[id]?.bds?.uuid == CONF.CONFIG.get('Websocket')['BDSUuid']){
                    return pl.sendForm(fm_transSrv, transSrv_run);
                }
                pl.sendModalForm('§c§l确认传送', `\n\n§b现在传送至 §r${BDSs[id].name} ?\n\n`, "§2§l✈ 立即传送", "§l取消", (pl, res)=>{
                    if (res == true){
                        mc.broadcast(`§e${pl.realName} 正在传送至 §r${BDSs[id].name}`);
                        pl.transServer(BDSs[id].bds.ip, BDSs[id].bds.port);
                    }else{
                        return pl.sendForm(fm_transSrv, transSrv_run);
                    }
                });
            }
            return false;
        }
    );
}


function getTpsColor(tps) {
    if (tps >= 18) {
        return '§2'; // 绿色：优秀
    } else if (tps >= 15) {
        return '§3'; // 蓝色：良好
    } else if (tps >= 10) {
        return '§e'; // 黄色：一般
    } else {
        return '§c'; // 红色：较差
    }
}
module.exports = transferMenu;